package city.spring.controller;

import city.spring.domain.entity.UnitmeasurementEntity;
import city.spring.service.UnitmeasurementService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：计量单位
 *
 * @author 谢金婷
 * @date 2020-05-06 09:29:54
 */
@RestController
@RequestMapping("/unitMeasurement")
public class UnitmeasurementController extends ApiController {
    private final UnitmeasurementService unitmeasurementService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<UnitmeasurementEntity, ?>> entityOrderFields;

    public UnitmeasurementController(UnitmeasurementService unitmeasurementService) {
        this.unitmeasurementService = unitmeasurementService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(UnitmeasurementEntity::getId);
        entityOrderFields.add(UnitmeasurementEntity::getMeasurement);
        entityOrderFields.add(UnitmeasurementEntity::getDeputyUnit);
        entityOrderFields.add(UnitmeasurementEntity::getRelationship);
    }

    /**
     * 获取全部的 <strong>计量单位</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, UnitmeasurementEntity entity) {
        LambdaQueryChainWrapper<UnitmeasurementEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>计量单位</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, UnitmeasurementEntity entity) {
        LambdaQueryChainWrapper<UnitmeasurementEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<UnitmeasurementEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>计量单位</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(unitmeasurementService.getById(id));
    }

    /**
     * 添加一个 <strong>计量单位</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody UnitmeasurementEntity entity) {
        unitmeasurementService.saveUnitmeasurement(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }


    /**
     * 修改一个 <strong>计量单位</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody UnitmeasurementEntity entity) {
        entity.setId(id);
        unitmeasurementService.updateUnitmeasurement(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /**
     * 删除一个 <strong>计量单位</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        unitmeasurementService.deleteUnitmeasurement(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>计量单位</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        unitmeasurementService.deleteUnitmeasurement(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<UnitmeasurementEntity> buildLambdaQuery(UnitmeasurementEntity entity) {
        LambdaQueryChainWrapper<UnitmeasurementEntity> lambdaQuery = unitmeasurementService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), UnitmeasurementEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMeasurement()), UnitmeasurementEntity::getMeasurement, entity.getMeasurement());
        return lambdaQuery;
    }

    /**
     * 查询单位，按创建时间排序
     *
     * @return
     */
    @GetMapping("/getUnitmeasurementByOrder")
    public Object getUnitmeasurementByOrder() {
        return success(unitmeasurementService.getUnitmeasurementByOrder());
    }

}
